<template>
  <div class="page-search">
    <WYForm v-bind="searchFormConfig" v-model="formData">
      <template #header> <h1>高级检索</h1></template>
      <template #footer>
        <div class="footer">
          <el-button
            size="mini"
            icon="el-icon-refresh"
            @click="handleReseyClick"
            >重置</el-button
          >
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-search"
            @click="handleQueryClick"
            >搜索</el-button
          >
        </div>
      </template>
    </WYForm>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import WYForm from '@/base-ui/from'

export default defineComponent({
  components: {
    WYForm
  },
  props: {
    searchFormConfig: {
      type: Object,
      reuqired: true
    }
  },
  emits: ['resetBtnClick', 'queryBtnClick'],
  setup(props, { emit }) {
    //双向绑定的属性应该由配置文件的field决定
    const formItems = props.searchFormConfig?.formItems ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.field] = ''
    }
    const formData = ref(formOriginData)
    //用户点击重置
    const handleReseyClick = () => {
      formData.value = formOriginData
      console.log(formOriginData)
      emit('resetBtnClick')
    }
    //用户点击搜索
    const handleQueryClick = () => {
      console.log('点击了搜索')
      emit('queryBtnClick', formData.value)
    }
    return { formData, handleReseyClick, handleQueryClick }
  }
})
</script>

<style scoped>
.footer {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
